/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-08-04
 * Time: 21:44
 */
public class test2 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if(head==null || n==0){
            return head;
        }
        ListNode pre = new ListNode(0);
        pre.next = head;
        ListNode slow = pre;
        ListNode fast = pre;
        // 提前移动n个距离
        for(int i=0;i<n;i++){
            fast = fast.next;
        }
        // 同时往前走
        while(fast.next!=null){
            fast = fast.next;
            slow = slow.next;
        }
        // 跳过这一个链表
        slow.next = slow.next.next;
        return pre.next;
    }

    class ListNode{
        int val;
        ListNode next;

        public ListNode(int val){
            this.val = val;
        }
    }
}
